package org.spbsu.apco.server;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Class to access {@link ApplicationContext} initialized after WebApplicationContext within the JVM.
 *
 * User: mikpan
 * Date: 12/21/12
 * Time: 11:36 AM
 */
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (ApplicationContextProvider.applicationContext != null) {
            throw new IllegalStateException("ApplicationContext is reset: " + applicationContext);
        }
        //noinspection AccessStaticViaInstance
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
